{% extends 'window.twig' %}
{% block body %}
    <section id="cd-timeline" class="cd-container data-view"{% if allowRecord %}{% else %} style="margin-bottom:0;"{% endif %}>
        <div class="task-info">
            <span class="task-state">{{ TaskState(task_data.task_state) }}</span>
            <div class="data-info">
                <table style="width: 100%">
                    <tr>
                        <td class="text-right" style="width: 20%;">任务编号&nbsp;:&nbsp;</td>
                        <td style="width: 35%;">{{ task_data.task_no }}</td>
                        <td class="text-right" style="width: 20%;">联系人姓名&nbsp;:&nbsp;</td>
                        <td style="width: 35%;">{{ task_data.contact_name }}</td>
                    </tr>
                    <tr>
                        <td class="text-right">联系人手机&nbsp;:&nbsp;</td>
                        <td>{{ task_data.phone }}</td>
                        <td class="text-right">联系人QQ&nbsp;:&nbsp;</td>
                        <td>{{ task_data.qq }}</td>
                    </tr>
                    <tr class="hidden">
                        <td class="text-right">任务状态&nbsp;:&nbsp;</td>
                        <td>{{ TaskState(task_data.task_state) }}</td>
                        <td class="text-right">创建时间&nbsp;:&nbsp;</td>
                        <td>{{ task_data.create_time }}</td>
                    </tr>
                    <tr>
                        <td class="text-right">创建者&nbsp;:&nbsp;</td>
                        <td>{{ task_data.nick_name }}</td>
                        <td class="text-right">咨询者年龄&nbsp;:&nbsp;</td>
                        <td>{{ task_data.age }}</td>
                    </tr>
                    <tr>
                        <td class="text-right">咨询者学历&nbsp;:&nbsp;</td>
                        <td>{{ task_data.education }}</td>
                        <td class="text-right">咨询者状态&nbsp;:&nbsp;</td>
                        <td>{{ task_data.contact_state }}</td>
                    </tr>
                    <tr class="hidden">
                        <td class="text-right">咨询者来源&nbsp;:&nbsp;</td>
                        <td colspan="3">{{ task_data.source }}</td>
                    </tr>
                    <tr>
                        <td class="text-right">搜索关键字&nbsp;:&nbsp;</td>
                        <td >{{ task_data.search_key }}</td>
                        <td class="text-right">意向专业&nbsp;:&nbsp;</td>
                        <td>{{ task_data.major }}</td>
                    </tr>
                    <tr>
                        <td class="text-right">联系时间&nbsp;:&nbsp;</td>
                        <td>{{ task_data.contact_time ? task_data.contact_time|date("Y-m-d H:i") : '-' }}</td>
                        <td class="text-right">咨询等级&nbsp;:&nbsp;</td>
                        <td>{{ task_data.task_level }}级(网咨){{ task_data.task_level1 }}级(咨询师)</td>
                    </tr>
                    <tr class="hidden">
                        <td class="text-right">推荐专业&nbsp;:&nbsp;</td>
                        <td>{{ task_data.recommend }}</td>
                        <td class="text-right">受访域名&nbsp;:&nbsp;</td>
                        <td>{{ task_data.visitdomain }}</td>
                    </tr>
                    {% for tdf in task_data_fields %}
                        <tr>
                            <td class="text-right">{{ tdf.title }}&nbsp;:&nbsp;</td>
                            <td colspan="3">{{ tdf.value }}</td>
                        </tr>
                    {% endfor %}
                </table>
            </div>
            <a href="#" id="pack_up" class="pack-up">展开</a>
            {% if task_data.has_distribution == 0  %}
                <a href="../task.setup.user?task={{ task_data.task_id }}" data-target="user.select"
                   class="setup pack-up setup-task {{ UserPermission('Setup_Task') }}" style="float: right;">分配</a>
            {% endif %}
        </div>
        {% for r in list %}
        <div class="cd-timeline-block">
            <div class="record-datetime">
                <span class="date">{{ r.record_time|date("m/d/y") }}</span>
                <span class="time">{{ r.record_time|date("H:i") }}</span>
            </div>
            <div class="cd-timeline-img cd-picture">
                <img src="{{ __URL__ }}/static/assets/app/cd-icon-picture.svg" alt="Picture">
            </div>
            <div class="cd-timeline-content">
                {#<h2>{{ r.record_time|date("Y-m-d H:i") }}</h2>#}
                <div class="log-main"><p>{{ r.record_log|raw }}</p></div>
                {#<a href="http://www.helloweba.com/view-blog-285.html" class="cd-read-more" target="_blank">阅读全文</a>#}
                <div class="text-right pull-right">记录人: <a href="#{{ r.nick_name }}">{{ r.nick_name }}</a></div>
            </div>
        </div>
        {% endfor %}
        <br>
    </section>
    <span id="_r_b"></span>
    {% if allowRecord %}
    <div id="remark" class="{{ UserPermission('RecordTaskLog') }}">
        <form action="../task.record" method="post" class="record-form">
            <input type="hidden" name="task_no" value="{{ task_data.task_no }}"/>
            <div class="editor">
                <textarea id="log_editor" name="log" style="height: 80px;max-height: 70px;" placeholder="此处输入记录内容"></textarea>
            </div>
            <div class="form-inline">
                <div class="pull-left">
                    <div class="input-group">
                        <div class="input-group-addon">下次提醒时间</div>
                        <input type="text" class="form-control datetime" name="notification_time" placeholder="为空则不提醒"
                               date-min="%y-%M-%d %H:%m:%s" date-format="yyyy-MM-dd HH:mm" style="width: 150px;  padding-left: 5px;">
                    </div>

                    <div class="input-group{{ UserPermission('SetupTaskState') }}" style="margin-left:5px;">
                        <div class="input-group-addon">级别</div>
                        <select class="form-control" name="task_level1" style="width:80px;">
                            <option value="0"{{ task_data.task_level1 == 0?' selected="selected"':''}}>未填写</option>
                            <option value="1"{{ task_data.task_level1 == 1?' selected="selected"':''}}>1级</option>
                            <option value="2"{{ task_data.task_level1 == 2?' selected="selected"':''}}>2级</option>
                            <option value="3"{{ task_data.task_level1 == 3?' selected="selected"':''}}>3级</option>
                            <option value="4"{{ task_data.task_level1 == 4?' selected="selected"':''}}>4级</option>
                        </select>
                    </div>
                    <div class="input-group{{ UserPermission('SetupTaskState') }}">
                        <div class="input-group-addon">任务状态</div>
                        <select class="form-control task_state_change" name="task_state" style="width: 110px;">
                            {% for state,tsl in task_state_list %}
                            <option value="{{ state }}"{{ task_data.task_state == state?' selected="selected"':''}}>{{ tsl }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="input-group task_state_change_view" style="display: none;">
                        <div class="input-group-addon">时间</div>
                        <input type="text" class="form-control datetime" name="notification_time_1" placeholder="为空则不提醒"
                               date-min="%y-%M-%d %H:%m:%s" date-format="yyyy-MM-dd HH:mm"  style="width: 150px;  padding-left: 5px;">
                    </div>
                </div>
                <div class="pull-right">
                    <button type="submit" class="btn btn-default ajax-post" target-form="record-form">记&nbsp;录</button>
                </div>
            </div>
        </form>
    </div>
    {% endif %}
{% endblock %}
{% block style %}
    <link rel="stylesheet" href="{{ __URL__ }}/static/assets/css/record.css"/>
{% endblock %}
{% block script %}
<script type="text/javascript" src="{{ __URL__ }}/static/assets/js/niceEditor.js"></script>
<script type="text/javascript">
        $(function(){
            var __newRecord = '{{ first_record }}';
            $('#pack_up').on('click',function(){
                if($(this).data('ex')){
                    $(this).prev().css('height','140px');
                    $(this).data('ex',false);
                    $(this).html('展开');
                    return false;
                }
                $(this).prev().css('height','auto');
                $(this).data('ex',true)
                $(this).html('缩小');
                return false;
            });
            $('.ajax-post').data('before',function(){
                var frm = $(this).parents('.record-form');
                if(__newRecord){
                    var task_level1 = frm.find('[name=task_level1]').val();
                    var task_state = frm.find('[name=task_state]').val();
                    if(task_level1 == '0' && task_state =='1'){
                        layer.msg("请修改任务级别或者任务状态",{time:1500});
                        return false;
                    }
                }
            });
            if($('#log_editor').length > 0){
                new nicEditor({maxHeight : 78}).addInstance('log_editor');
            }
            var __taskStateChange = function(){
                if($(this).val() == 4||$(this).val() == 7) $('.task_state_change_view').show();
                else  $('.task_state_change_view').hide();
            };
            $('.task_state_change').change(__taskStateChange);
            __taskStateChange.call($('.task_state_change'));
            util.setTitle(window,'任务详情:{{ task_data.phone }}-{{ task_data.contact_name }}');
            /**
             *
             * @param userList 用户选择界面
             * @param callback 回调
             * @returns {boolean}
             */
            var selectUser = function(userList,callback){
                var opts = {
                    type: 2,
                    fix: false, //不固定
                    area: ['300px', '120px'],
                    topIndex:9999,
                    maxmin: false,
                    shade:[0.8, '#393D49'],
                    offset:'tc',
                    content:userList,
                    title:'选择用户',
                    end:function(tag){
                        if(tag && tag['uid'].length > 0){
                            if(tag['uid'].length > 1) {
                                layer.msg('只能将任务分配给1个用户')
                                return false;
                            }
                            callback.call(this,tag);
                        }
                    }
                }
                layer.open(opts);
                return false;
            };
            var setupTask = function(url,uid){
                $.post(url,{uid:uid},function(json){
                    if(json['code'] == 0){
                        layer.msg('分配任务成功',{time:1000});
                        setTimeout(function () {
                            location.reload();
                        }, 1000);
                    }else{
                        layer.msg(json['msg'],{time:1000});
                    }
                },'json').error(function(){
                            alert('分配任务失败,请重试');
                        });
            };
            $('.setup-task').on('click',function(){
                var me = $(this);
                var url = me.attr('href');
                selectUser(me.attr('data-target'),function(data){
                    setupTask(url,data['uid'][0]);
                });
                return false;
            });
        });
</script>
{% endblock %}